
package xib.core;

import java.util.ArrayList;
import xib.parserTools.interfaces.IChapterData;
import xib.parserTools.interfaces.IPageData;
import xib.utils.EncodedString;

/**
 *
 * @author xavierds
 */
final class ChapterXib {

    /**
     * Attribute with the number of the chapter.
     */
    private int number;

    /**
     * Attribute with the list of titles from the chapter.
     */
    private ArrayList<EncodedString> titles;

    /**
     * Attribute with the list of pages of the chapter.
     */
    private ArrayList<PageXib> pages;

    private int totalPanels;

    /**
     * Constructor by default
     * @param chapter
     */
    public ChapterXib(IChapterData chapter){
        chapter.validate();

        titles = chapter.getTitles();
        number = chapter.getChapterNumber();
        totalPanels = 0;

        ArrayList<IPageData> list = chapter.getPages();
        pages = new ArrayList<PageXib>();
        for(int i=0; i < list.size(); i++){
            pages.add(new PageXib(list.get(i)));
            totalPanels += pages.get(i).getQuantityOfPanels();
        }     
    }

    /**
     * Obtain the number of the chapter.
     * @return integer
     */
    public int getNumber(){
        return number;
    }

    /**
     * Obtain the total pages of the chapter.
     * @return integer
     */
    public int getTotalPages(){
        return pages.size();
    }

    public int getTotalPanels(){
        return totalPanels;
    }

    /**
     * Obtain the PageXib int the position pos.
     * @param pos must be between 1 and the quantity of pages.
     * @return PageXib
     * @throws IllegalArgumentException if the position is out of range
     */
    public PageXib getPageAt(int pos){
        if(pos < 1 && pos > pages.size())
            throw new IllegalArgumentException("No page exists in this " +
                    "position, Position must be between 1 and " + pages.size());
        return pages.get(pos - 1);
    }

}
